package com.imagine.service.impl;

import com.imagine.common.Global;
import com.imagine.dal.QuerySinaStocks;
import com.imagine.entity.BaseResult;
import com.imagine.entity.Stock;
import com.imagine.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by liangyue on 2017/10/2.
 */
@Service
public class StockServiceImpl implements StockService {
    @Autowired
    QuerySinaStocks querySinaStocks;

    public BaseResult queryStock(String stockCode){
        BaseResult result = new BaseResult();
        Stock stock = null;
        try {
            stock = querySinaStocks.querySinaStocks(stockCode);
            if (stock == null){
                result.error("没有该股票信息");
                return result;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        result.success(stock);
        return result;
    }
    @JmsListener(destination="stock_search_queue")
    @Override
    public Stock queryStock2(String stockCode){
        Stock stock = null;
        try {
            stock = querySinaStocks.querySinaStocks(stockCode);
            if (stock == null){
                stock = new Stock();
            }
            querySinaStocks.querySinaStockImage(stockCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stock;
    }
    @Override
    public BaseResult queryStockImage(String stockCode){
        BaseResult result = new BaseResult();
        try {
             int re = querySinaStocks.querySinaStockImage(stockCode);
             if (re == -1){
                 result.error("未获取到最新k线信息");
             }
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.success();
        return result;
    }
    @Override
    public BaseResult queryAllStock(){
        BaseResult result = new BaseResult();
        try {
            ArrayList<Stock> list = Global.arrayList;
            result.success(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
